package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Coupon;
import com.api.syh56.shengyunhui.entity.CouponDet;
import com.api.syh56.shengyunhui.mapper.CouponMapper;
import com.api.syh56.shengyunhui.service.CouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public void insert() {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));

        HashMap<Short, Integer> couponNum = new HashMap<>();
        couponNum.put(Constants.couponCode.TEN,40);
        couponNum.put(Constants.couponCode.TWENTY,30);
        couponNum.put(Constants.couponCode.FIFTY,20);
        couponNum.put(Constants.couponCode.HUNDRED,10);
        couponNum.put(Constants.couponCode.TWO_HUNDRED,5);
        couponNum.put(Constants.couponCode.FIVE_HUNDRED,2);

        HashMap<Short, BigDecimal> couponSum = new HashMap<>();
        couponSum.put(Constants.couponCode.TEN,BigDecimal.valueOf(10));
        couponSum.put(Constants.couponCode.TWENTY,BigDecimal.valueOf(20));
        couponSum.put(Constants.couponCode.FIFTY,BigDecimal.valueOf(50));
        couponSum.put(Constants.couponCode.HUNDRED,BigDecimal.valueOf(100));
        couponSum.put(Constants.couponCode.TWO_HUNDRED,BigDecimal.valueOf(200));
        couponSum.put(Constants.couponCode.FIVE_HUNDRED,BigDecimal.valueOf(500));

        LocalDateTime now = LocalDateTime.now();
        for(Short key : couponNum.keySet()){
            Coupon coupon = Coupon.builder()
                    .couponCode(key)
                    .couponType(Constants.couponType.FULL_REDUCTION)
                    .couponStatus(Constants.couponStatus.EFFECTIVE)
                    .sum(couponSum.get(key))
                    .startDate(now)
                    .endDate(now.plusDays(100))
                    .remainingCoupon(couponNum.get(key))
                    .totalCoupon(couponNum.get(key))
                    .uid(uid)
                    .build();
            couponMapper.insert(coupon);
        }

    }

    @Override
    public int getCouponNum(Long uid) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Coupon::getUid,uid)
                .select(Coupon::getRemainingCoupon);
        List<Integer> nums = couponMapper.selectObjs(wrapper);
        int sum = 0;
        for (int num : nums){
            sum += num;
        }
        System.out.println(sum);
        return sum;
    }
}
